﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace HKS
{
    /*
        //Storage class USAGE
        
        Storage s = new Storage();
     
        // Get off-file filename (e.g. 0006.sampling.5)
        String offFileName = Path.GetFileNameWithoutExtension(args[0]);
            
        // save eigenvector
        double[] eigenVector = new double[3];
        eigenVector[0] = 1.2234;
        eigenVector[1] = 5.28937;
        eigenVector[2] = 2.0938;
        s.saveEigenVector(offFileName, eigenVector);

        // save HKS Value
        s.saveHKSValue(offFileName, 0.1, 1.523324);
        
        // Fetch data from saved files
        Console.WriteLine(s.getHKSValue(offFileName, 0.1));

        double[] vector = s.getEigenVector(offFileName);
        Console.WriteLine(vector[0] + " - " + vector[1] + " - " + vector[2]);
    */


    class Storage
    {
        // stream to store data in a file
        private String outputDir = @"C:\Users\Frank Borgonjen\Documents\MultimediaRetrieval\output";
        private Dictionary<String, double[]> dictionary = null;

        /**
         * HKS Values
         */
        public void saveHKSValues(String offFileName, double[] values)
        {
            loadHKSDictionary();
            dictionary.Remove(offFileName);
            dictionary.Add(offFileName, values);

            // serialize dictionary
            String dictionaryFile = outputDir + "/values.hks";
            serializeObject(dictionary, dictionaryFile);
            Console.WriteLine("Dictionary Saved");
        }

        public double[] getHKSValues(String offFileName)
        {
            loadHKSDictionary();
            return dictionary[offFileName];
        }

        /**
         * Eigen Vectors
         */
        public void saveEigenVector(String offFileName, double[] vector)
        {
            // serialize vector
            String vectorFile = outputDir + "/" + offFileName + ".vctr";
            serializeObject(vector, vectorFile);
            Console.WriteLine("Eigenvector Saved");
        }

        public double[] getEigenVector(String offFileName)
        {
            double[] result = null;

            String vectorFile = outputDir + "/" + offFileName + ".vctr";
            if (File.Exists(vectorFile))
            {
                result = (double[])deSerializeObject(vectorFile);
            }
            return result;
        }


        private void loadHKSDictionary()
        {
            if (dictionary == null)
            {
                String dictionaryFile = outputDir + "/values.hks";
                if (File.Exists(dictionaryFile))
                {
                    dictionary = (Dictionary<String, double[]>) deSerializeObject(dictionaryFile);
                }
                else
                {
                    dictionary = new Dictionary<String, double[]>();
                }
            }
        }


        private void serializeObject(Object o, String outputFileName)
        {
            Stream stream = File.Open(outputFileName, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, o);
            stream.Close();
        }

        private Object deSerializeObject(String fileName)
        {
            Stream stream = File.Open(fileName, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            Object result = bFormatter.Deserialize(stream);
            stream.Close();

            return result;
        }
    }
}
